﻿using Caliburn.Micro;
using OPSYS_Schematic.Presentation.Infrastructure;
using OPSYS_Schematic.UIInfrastructure;

namespace OPSYS_Schematic.Presentation.Parts
{
    //[MenuPart(PreviousMenu = WorkbenchName.ToolPart)]
    [MenuPart(BaseMenu = WorkbenchName.GeneralPart, PreviousMenu = WorkbenchName.ViewPart)]
    public class WindowPart : MenuPart, IHandle<DockWindowLoadedMessage>
    {
        #region Field

        private CollectionMerger<IMenuPart> merger;
        private CollectionSyncer<IDockScreen, IMenuPart> syncer;

        #endregion

        #region Constructor

        public WindowPart()
            : base(WorkbenchName.WindowPart)
        {
            IoC.Get<IEventAggregator>().Subscribe(this);
        }

        #endregion

        #region IHandle<DockWindowLoadedMessage> Members

        public void Handle(DockWindowLoadedMessage message)
        {
            var manager = IoC.Get<IDockScreenManager>();
            syncer = new CollectionSyncer<IDockScreen, IMenuPart>(manager.Documents, (item) =>
                                                                                         {
                                                                                             return
                                                                                                 new MenuPart(
                                                                                                     item.Name, (i) =>
                                                                                                                    {
                                                                                                                        var
                                                                                                                            menu
                                                                                                                                =
                                                                                                                                i
                                                                                                                                as
                                                                                                                                MenuPart;
                                                                                                                        menu
                                                                                                                            .
                                                                                                                            IsChecked
                                                                                                                            =
                                                                                                                            !menu
                                                                                                                                 .
                                                                                                                                 IsChecked;
                                                                                                                        if
                                                                                                                            (
                                                                                                                            menu
                                                                                                                                .
                                                                                                                                IsChecked)
                                                                                                                        {
                                                                                                                            IoC
                                                                                                                                .
                                                                                                                                Get
                                                                                                                                <
                                                                                                                                    IDockScreenManager
                                                                                                                                    >
                                                                                                                                ()
                                                                                                                                [
                                                                                                                                    menu
                                                                                                                                        .
                                                                                                                                        Name
                                                                                                                                ]
                                                                                                                                .
                                                                                                                                Show
                                                                                                                                ();
                                                                                                                        }
                                                                                                                        else
                                                                                                                        {
                                                                                                                            IoC
                                                                                                                                .
                                                                                                                                Get
                                                                                                                                <
                                                                                                                                    IDockScreenManager
                                                                                                                                    >
                                                                                                                                ()
                                                                                                                                [
                                                                                                                                    menu
                                                                                                                                        .
                                                                                                                                        Name
                                                                                                                                ]
                                                                                                                                .
                                                                                                                                Close
                                                                                                                                ();
                                                                                                                        }
                                                                                                                    })
                                                                                                     {
                                                                                                         IsCheckable = true
                                                                                                     };
                                                                                         });
            merger = new CollectionMerger<IMenuPart>((this as IObservableParent<IMenuPart>).Items, syncer.SyncCollection);
        }

        #endregion
    }
}